package com.css.fxfzaqbz.modules.qhmapfiles.controller;

import com.css.fxfzaqbz.modules.qhmapfiles.repository.entity.QhMapFilesEntity;
import com.css.fxfzaqbz.modules.qhmapfiles.service.QhMapFilesService;
import com.css.fxfzaqbz.modules.qhmapfiles.viewobjects.QhMapFilesQueryParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author zyt
 * @date 2022-09-26
 */
@Slf4j
@RestController
@RequestMapping("/qh/qhMapFiles")
public class QhMapFilesController {
    @Autowired
    private QhMapFilesService qhMapFilesService;

    @Autowired
    private QhMapFilesController qhMapFilesController;

    //读取文件保存数据
    public void readMapFilesSaveData() throws Exception {
        //查询所有未保存的文件
        List<QhMapFilesEntity> list = qhMapFilesService.searchAllUnsaveQhMapFiles();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                QhMapFilesEntity qhMapFilesEntity = list.get(i);
                String type = qhMapFilesEntity.getType();//上传类型（SITE_）
                String province = qhMapFilesEntity.getProvince();
                String city = qhMapFilesEntity.getCity();
                String area = qhMapFilesEntity.getArea();
                //判断是否未保存文件--上传类型（SITE_）
                int fileCount = qhMapFilesService.lookUpMapFiles("0",type,null,province,city,area);
                if(fileCount>0){
                    //根据类型保存到数据库不同表
                    String resMsg = qhMapFilesService.saveQhMapFilesToDatabaseByType(qhMapFilesEntity);
                    System.out.println(new Date() + "----------" + resMsg);
                }
            }
        }
    }

    //根据类型读取文件保存数据
    public void readMapFilesSaveDataByType(String type) throws Exception {
        //根据类型查询未保存的文件
        QhMapFilesQueryParams queryParams = new QhMapFilesQueryParams();
        queryParams.setType(type);//上传类型（SITE_）
        queryParams.setIsDelete("0");//未删除
        queryParams.setIsSave("0");//未保存
        List<QhMapFilesEntity> list = qhMapFilesService.searchQhMapFilesByParams(queryParams);
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                QhMapFilesEntity qhMapFilesEntity = list.get(i);
                String province = qhMapFilesEntity.getProvince();
                String city = qhMapFilesEntity.getCity();
                String area = qhMapFilesEntity.getArea();
                //判断是否未保存文件--上传类型（SITE_）
                int fileCount = qhMapFilesService.lookUpMapFiles("0",type,null,province,city,area);
                if(fileCount>0){
                    //根据类型保存到数据库不同表
                    String resMsg = qhMapFilesService.saveQhMapFilesToDatabaseByType(qhMapFilesEntity);
                    System.out.println(new Date() + "----------" + resMsg);
                }
            }
        }
    }

    //定时任务(0 0 0 * * ?)每天0点执行
    /*@Scheduled(cron = "0 0 0 * * ?")*/
    /*@Scheduled(cron = "${scheduled.cron}")
    public void doTask() {
        System.out.println(new Date() + "定时开始读取文件并保存数据任务！");
        try {
            //读取文件保存数据
            qhMapFilesController.readMapFilesSaveData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(new Date() + "定时结束读取文件并保存数据任务！");
    }*/

    /*@Scheduled(cron = "${scheduled.cron}")
    public void doTask1() {
        System.out.println(new Date() + "PGA----定时开始读取文件并保存数据任务！");
        try {
            //根据类型读取文件保存数据（PGA）
            qhMapFilesController.readMapFilesSaveDataByType("PED_");//上传类型（SITE_）
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(new Date() + "PGA----定时结束读取文件并保存数据任务！");
    }

    @Scheduled(cron = "${scheduled.cron}")
    public void doTask2() {
        System.out.println(new Date() + "等级图矢量点----定时开始读取文件并保存数据任务！");
        try {
            //根据类型读取文件保存数据（等级图矢量点）
            qhMapFilesController.readMapFilesSaveDataByType("RLM_");//上传类型（SITE_）
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(new Date() + "等级图矢量点----定时结束读取文件并保存数据任务！");
    }

    @Scheduled(cron = "${scheduled.cron}")
    public void doTask3() {
        System.out.println(new Date() + "等级图矢量面----定时开始读取文件并保存数据任务！");
        try {
            //根据类型读取文件保存数据（等级图矢量面）
            qhMapFilesController.readMapFilesSaveDataByType("RLMPolygon_");//上传类型（SITE_）
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(new Date() + "等级图矢量面----定时结束读取文件并保存数据任务！");
    }*/
}